package com.example.demo.core.response;

import com.fasterxml.jackson.annotation.JsonPropertyOrder;
import lombok.Data;

@Data
@JsonPropertyOrder(value = "code,msg,data")
public class ApiResponse<T> {

    private static final String ERROR_CODE = "0001";
    private static final String ERROR_MSG = "error";
    private static final String SUCCESS_CODE = "0000";
    private static final String SUCCESS_MSG = "success";

    private String code;
    private String msg;
    private T data;

    public ApiResponse(String code, String msg, T data) {
        this.code = code;
        this.msg = msg;
        this.data = data;
    }

    public static <T> ApiResponse<T> ok() {
        return new ApiResponse<>(SUCCESS_CODE, SUCCESS_MSG, null);
    }

    public static <T> ApiResponse<T> ok(String msg, T data) {
        return new ApiResponse<>(SUCCESS_CODE, msg, data);
    }

    public static <T> ApiResponse<T> ok(T data) {
        return new ApiResponse<>(SUCCESS_CODE, SUCCESS_MSG, data);
    }

    public static <T> ApiResponse<T> fail() {
        return new ApiResponse<>(ERROR_CODE, ERROR_MSG, null);
    }

    public static <T> ApiResponse<T> fail(String msg) {
        return new ApiResponse<>(ERROR_CODE, msg, null);
    }

    public static <T> ApiResponse<T> fail(String msg, T data) {
        return new ApiResponse<>(ERROR_CODE, msg, data);
    }

    public static <T> ApiResponse<T> fail(String code, String msg, T data) {
        return new ApiResponse<>(code, msg, data);
    }

    public static <T> ApiResponse<T> fail(String code, String msg) {
        return new ApiResponse<>(code, msg, null);
    }
}
